home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBCLOSE.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  100 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbclose.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STRING
  11. #include <string.h>
  12. #endif
  13.  
  14. /* library headers */
  15. #include <btree.h>
  16. #include <lseq.h>
  17.  
  18. /* local headers */
  19. #include "cbase_.h"
  20.  
  21. /*man---------------------------------------------------------------------------
  22. NAME
  23.      cbclose - close cbase
  24.  
  25. SYNOPSIS
  26.      #include <cbase.h>
  27.  
  28.      int cbclose(cbp)
  29.      cbase_t *cbp;
  30.  
  31. DESCRIPTION
  32.      The cbclose function causes any buffered data for cbase cbp to be
  33.      written out and the cbase to be unlocked and closed.
  34.  
  35.      cbclose will fail if one or more of the following is true:
  36.  
  37.      [EINVAL]       cbp is not a valid cbase pointer.
  38.      [CBENOPEN]     cbp is not open.
  39.  
  40. SEE ALSO
  41.      cbcreate, cbopen, cbsync.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise,
  45.      a value of -1 is returned, and errno set to indicate the error.
  46.  
  47. ------------------------------------------------------------------------------*/
  48. #ifdef AC_PROTO
  49. int cbclose(cbase_t *cbp)
  50. #else
  51. int cbclose(cbp)
  52. cbase_t *cbp;
  53. #endif
  54. {
  55.     int i = 0;
  56.  
  57.     /* validate arguments */
  58.     if (!cb_valid(cbp)) {
  59.         errno = EINVAL;
  60.         return -1;
  61.     }
  62.  
  63.     /* check if not open */
  64.     if (!(cbp->flags & CBOPEN)) {
  65.         errno = CBENOPEN;
  66.         return -1;
  67.     }
  68.  
  69.     /* flush buffers and unlock file */
  70.     if (cblock(cbp, CB_UNLCK) == -1) {
  71.         CBEPRINT;
  72.         return -1;
  73.     }
  74.  
  75.     /* close record file */
  76.     if (lsclose(cbp->lsp) == -1) {
  77.         CBEPRINT;
  78.         return -1;
  79.     }
  80.  
  81.     /* close key files */
  82.     for (i = 0; i < cbp->fldc; ++i) {
  83.         if (cbp->fldv[i].flags & CB_FKEY) {
  84.             if (btclose(cbp->btpv[i]) == -1) {
  85.                 CBEPRINT;
  86.                 return -1;
  87.             }
  88.         }
  89.     }
  90.  
  91.     /* free memory allocated for cbase */
  92.     cb_freemem(cbp);
  93.  
  94.     /* scrub slot in cbb table then free it */
  95.     memset(cbp, 0, sizeof(*cbb));
  96.     cbp->flags = 0;
  97.  
  98.     return 0;
  99. }
  100.